package com.hellobike.haxing.modules.sharecampaign.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.sharecampaign.entity.ShareCampaign;
import com.hellobike.haxing.modules.sharecampaign.mapper.ShareCampaignMapper;
import com.hellobike.haxing.modules.sharecampaign.service.IShareCampaignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: t_share_campaign
 * @Author: haxing-boot
 * @Date: 2020-02-28
 * @Version: V1.0
 */
@Service
@Slf4j
public class ShareCampaignServiceImpl extends ServiceImpl<ShareCampaignMapper, ShareCampaign> implements IShareCampaignService {

    @Autowired
    private ShareCampaignMapper shareCampaignMapper;

    @Override
    public Result saveShareCampaign(ShareCampaign shareCampaign) {
        if (!checkUniqueActive(shareCampaign)) {
            return Result.error("只能有一个活动为开启状态");
        }
        log.info("insert-log:{}" + JSON.toJSONString(shareCampaign));
        shareCampaignMapper.insert(shareCampaign);
        return Result.ok("添加成功！");
    }

    @Override
    public Result updateShareCampaign(ShareCampaign shareCampaign) {
        if (!checkUniqueActive(shareCampaign)) {
            return Result.error("只能有一个活动为开启状态");
        }
        shareCampaignMapper.updateById(shareCampaign);
        return Result.ok("编辑成功！");
    }

    @Override
    public Result<?> changeState(Long id, Integer status) {
        if (status == 0) {
            QueryWrapper<ShareCampaign> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ShareCampaign::getStatus, 0);
            List<ShareCampaign> shareCampaignList = shareCampaignMapper.selectList(queryWrapper);
            boolean checked = true;
            if (shareCampaignList.size() > 1) {
                checked = false;
            } else if (shareCampaignList.size() == 1) {
                checked = id.equals(shareCampaignList.get(0).getId());
            }
            if (!checked) {
                return Result.error("同时只允许开启一个活动");
            }
        }
        ShareCampaign shareCampaign = new ShareCampaign();
        shareCampaign.setId(id);
        shareCampaign.setStatus(status);
        shareCampaignMapper.updateById(shareCampaign);
        return Result.ok("状态修改成功");
    }

    //检查是否唯一开启的
    private boolean checkUniqueActive(ShareCampaign shareCampaign) {
        if (shareCampaign.getStatus() == 0) {
            QueryWrapper<ShareCampaign> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ShareCampaign::getStatus, 0);
            List<ShareCampaign> activeShareCampaigns = shareCampaignMapper.selectList(queryWrapper);
            if (activeShareCampaigns.size() > 1) {
                return false;
            } else if (activeShareCampaigns.size() == 1) {
                if (shareCampaign.getId() == null) {
                    return false;
                } else {
                    return shareCampaign.getId().equals(activeShareCampaigns.get(0).getId());
                }
            } else {
                return true;
            }
        }
        return true;
    }
}
